package com.tl.demo;

import java.io.FileInputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.textmining.text.extraction.WordExtractor;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONObject;
import cn.com.wfc.util.JsonUtil;
import cn.com.zp.search.BusinessService;
import cn.com.zprk.XsdUtil;
import cn.com.zprk.ZpxxBO;
import cn.com.zprk.xsd.Table;
import cn.com.zprk.xsd.TableItem;
import cn.com.zprk.xsd.types.FieldLuenceType;

import com.cdoframework.cdolib.base.Return;
import com.cdoframework.cdolib.data.cdo.CDO;
import com.cdoframework.cdolib.net.MemCacheClient;
import com.ibm.icu.text.SimpleDateFormat;
import com.jincin.search.schema.Field;
import com.jincin.search.schema.FieldGroup;
import com.jincin.search.schema.Page;
import com.jincin.search.schema.Query;
import com.jincin.search.schema.types.FieldGroupOperatorType;

public class Demo {

	private static String index = "zp/dw/";
	private static Logger log = Logger.getLogger(ZpxxBO.class);
	
//	public static void main(String[] args) throws Exception {
//		
//		FileInputStream in = new FileInputStream("d:\\test.doc");
//		WordExtractor extractor = new WordExtractor();
//		String str = extractor.extractText(in);
//		// System.out.println("the result length is"+str.length());
//		System.out.println(str);
//		
//		//		DES des = new DES();
//		//		byte[] bytes = "AAAAAAAA".getBytes();
//		//		des.setKey(bytes);
//		////		String dateTime = Misc.getDateTime();
//		////		System.out.println(dateTime);
//		//		byte[] encrypt = des.encrypt("tl".getBytes());
//		//		
//		//		String str = Utility.bytesToHexString(encrypt);
//		//		System.out.println(str);
//		//		byte[] hexStringToBytes = Utility.hexStringToBytes(str);
//		//		byte[] decrypt = des.decrypt(hexStringToBytes);
//		//		System.out.println(new String(decrypt));
//	}
	
	/**
	 * 用户向数据库和索引添加数据
	 * @author zhengfen
	 * @date 2014-08-15
	 * 输入参数:
	 * @param cdoRequest
	 * @param cdoResponse
	 * @return
	 */
	public static Return addDemo(){
		MemCacheClient cache = BusinessService.getInstance().getCache();
//		cache.deleteCacheValue("tl");
//		cache.putCacheValue("tl", "wwwwwww", 5);
		String str = (String) cache.getCacheValue("tl");
		System.out.println(str);
		//根据参数组成Add类
		
//		String[] arr = {"abvewrewdsfsdfsereasfwerfw1101","单位010","北京001","单位性质001","行业001"};
//		String[] arrf = {"DWID","DWMC","DWSZSDMMC","DWXZYJDMMC","HYYJDMMC"};
//		for(int i=0;i<10;i++){
//			FieldGroup fieldGroup = new FieldGroup();
//			for(int j=0;j<arr.length;j++){
//				Field f = new Field(arrf[j],arr[j]+j);
//				fieldGroup.addField(f);
//			}
//			Add add = new Add(index,fieldGroup);
//			BusinessService.getInstance().sendRequest(add);
//		}
//		
//		FieldGroup fieldGroup = new FieldGroup();
//		for(int j=0;j<arr.length;j++){
//			Field f = new Field(arrf[j],arr[j]+j);
//			fieldGroup.addField(f);
//		}
//		Add add = new Add(index,fieldGroup);	
//		BusinessService.getInstance().sendRequest(add);
		return null;
	}
	
	public static Return findDemo(){
		try {
			Field field1 = new Field("DWID","");
			FieldGroup fieldGroup = new FieldGroup();
			fieldGroup.addField(field1);
			fieldGroup.setOperator(FieldGroupOperatorType.OR);
			Vector<FieldGroup> fieldGroupList = new Vector<FieldGroup>();
			fieldGroupList.add(fieldGroup);
			Page page = new Page(0,20);
					
			Query queryObj = new Query(index,fieldGroupList,page);
			//调用lucene服务
			CDO res = new CDO();
			BusinessService.getInstance().sendRequest(queryObj,res);
			CDO resCDO = res.getCDOValue("cdoResult");
			if(resCDO.exists("cdosResult")){
				CDO[] cdoArr = resCDO.getCDOArrayValue("cdosResult");
				JSONArray arr = new JSONArray();
				List list = new ArrayList();
				for(CDO c:cdoArr){
					c.removeField("AllFieldFlag");
					String tempJson = c.toJSONString();
					tempJson = tempJson.replace("\\", "");
					JSONObject jObj = new JSONObject(tempJson);
					HashMap hashMap = jObj.toHashMap();
					list.add(hashMap);
				}
				String json = JsonUtil.list2json(list);
				System.out.println(json);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Return deleteDemo(){
		Field field1 = new Field("DWID","");
		FieldGroup fieldGroup = new FieldGroup();
		fieldGroup.addField(field1);
		com.jincin.search.schema.Delete delObj = new com.jincin.search.schema.Delete(index,fieldGroup);
		BusinessService.getInstance().sendRequest(delObj);
		return null;
	}
	
	/**
	 * 批量添加索引字段
	 * @author tl
	 * @time 2014-11-13
	 */
	public static void addIndexField(){
		Map<Long,String> map = new HashMap<Long,String>();
		map.put(68L, "jy_zpfb_dw");
		map.put(71L, "jy_zpfb_zw");
		map.put(72L, "jy_zpfb_zph");
		map.put(73L, "jy_zpfb_xjh");
		
		for(Map.Entry<Long, String> entry:map.entrySet()){
			Long key = entry.getKey();
			String val = entry.getValue();
			Table table = XsdUtil.getTableByName(val, "table_fb.xml");
			TableItem[] tableItem = table.getTableItem();
			String str = "";
			int index = 1;
			for(TableItem ti:tableItem){
				cn.com.zprk.xsd.Field f = ti.getField();
				if(f!=null && FieldLuenceType.VALUE_1.equals(f.getLuence())){
					str += f.getName()+", ";
					send(key,f.getName(),f.getValue());
					index++;
				}
				
			}
			log.info(val+" 有"+index+"添加到lucene,见:"+str);
		}
	}
	
	/***
	 * @author tl
	 * @time 2014-11-10
	 */
	public static void send(Long lSytemId,String strIndexName,String strName){
		CDO req = new CDO();
		CDO res = new CDO();
		req.setStringValue("strServiceName", "SearcherService");
		req.setStringValue("strTransName", "addIndexColumn");
		req.setStringValue("strIndexName",strIndexName);
		req.setStringValue("strIndexType", "String");
		req.setStringValue("strName", strName);
		req.setIntegerValue("nIndex",1);
		req.setIntegerValue("nStore",1);
		req.setLongValue("lSytemId",lSytemId) ;
		Return ret = BusinessService.getInstance().handleTrans(req, res);
	}
	
	public static void test(){
		System.out.println("=============算术右移 >> ===========");
		int i=0xC0000000;
		System.out.println("移位前：i= "+i+" = "+Integer.toBinaryString(i)+"(B)");
		i=i>>28;
		System.out.println("移位后：i= "+i+" = "+Integer.toBinaryString(i)+"(B)");
		
		System.out.println("---------------------------------");
		
		int j=0x0C000000;
		System.out.println("移位前：j= "+j+" = "+Integer.toBinaryString(j)+"(B)");
		j=j>>24;
		System.out.println("移位后：j= "+j+" = "+Integer.toBinaryString(j)+"(B)");

		System.out.println("\n");
		System.out.println("==============逻辑右移 >>> =============");
		int m=0xC0000000;
		System.out.println("移位前：m= "+m+" = "+Integer.toBinaryString(m)+"(B)");
		m=m >>> 28;
		System.out.println("移位后：m= "+m+" = "+Integer.toBinaryString(m)+"(B)");
		System.out.println("---------------------------------");
		
		int n=0x0C000000;
		System.out.println("移位前：n= "+n+" = "+Integer.toBinaryString(n)+"(B)");
		n=n>>24;
		System.out.println("移位后：n= "+n+" = "+Integer.toBinaryString(n)+"(B)");
		
		System.out.println("\n");
		System.out.println("==============移位符号的取模===============");
		int a=0xCC000000;
		System.out.println("移位前：a= "+a+" = "+Integer.toBinaryString(a)+"(B)");
		System.out.println("算术右移32：a="+(a>>32)+" = "+Integer.toBinaryString(a>>32)+"(B)");
		System.out.println("逻辑右移32：a="+(a>>>32)+" = "+Integer.toBinaryString(a>>>32)+"(B)");
	}
	
	public static void main(String[] args) throws Exception {
//			SimpleDateFormat   df   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
//		  Date   begin=df.parse("2006-10-11 20:00:00");   
//		  Date   end   =   df.parse("2006-10-12 8:00:00");   
//		  long   between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒   
//		  int   day=(int) (between/(24*3600));   
//		  int   hour=(int) (between%(24*3600)/3600);   
//		  int   minute=(int) (between%3600/60);   
//		  int   second=(int) (between%60); 
//		  System.out.println(hour);
		  
		  String t1="tl",t2 = "tl";
		  System.out.println(t1);
		  System.out.println(t2);
	}
}
